package com.nbjtjc.safe.api.index;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.danger.DangerInspectPublicMapper;
import com.nbjtjc.safe.mapper.sys.SysPostMapper;
import com.nbjtjc.safe.mapper.sys.SysUserPostMapper;
import com.nbjtjc.safe.model.danger.DangerInspectPublic;
import com.nbjtjc.safe.model.sys.SysPost;
import com.nbjtjc.safe.model.sys.SysUserPost;
import com.nbjtjc.safe.service.index.IndexService;
import com.nbjtjc.safe.util.DateUtils;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * @auther guqianbin
 * @date 2018/7/16 14:55
 * 隐患首页统计相关的接口
 **/
@RestController
@RequestMapping("/index")
@CrossOrigin("*")
public class SafeIndexController {
    @Resource(name = "safeIndexService")
    private IndexService indexService;

    @Autowired
    private SysUserPostMapper sysUserPostMapper;

    @Autowired
    private DangerInspectPublicMapper dangerInspectPublicMapper;

    @Autowired
    private SysPostMapper sysPostMapper;
    @GetMapping("/safeIndex/{year}")
    public Result safeIndex(@PathVariable("year") int year) throws ParseException {
        Result result = new Result();
        result.setData(indexService.yearSafeSummary(year));
        return result;
    }

    @GetMapping("/safeWarningTrend/{year}")
    public Result safeWarningTrend(@PathVariable("year") int year) {
        Result result = new Result();
        result.setData(indexService.safeWarningTrend(year));
        return result;
    }

    @GetMapping("/analysisHiddenDangers/{year}")
    public Result analysisHiddenDangers(@PathVariable("year") int year) {
        Result result = new Result();
        result.setData(indexService.analysisHiddenDangers(year));
        return result;
    }

    @GetMapping("/indexTaskInfo/{year}")
    public Result indexTaskInfo(@PathVariable("year") int year) {
        Result result = new Result();
        result.setData(indexService.indexTaskInfo(year));
        return result;
    }

    @GetMapping("/userSelfInspectInfo/")
    public Result userSelfInspectInfo() {
        Result result = new Result();
        Calendar calendar = Calendar.getInstance();
        List<Date> dates = DateUtils.getYearRange(calendar.get(Calendar.YEAR));
        List<SysUserPost> sysUserPosts =
                sysUserPostMapper.findByUserId(MyShiroUtils.getSessionUserId());
        DangerInspectPublic dangerInspectPublic = new DangerInspectPublic();
        dangerInspectPublic.setStartDate(dates.get(0));
        dangerInspectPublic.setEndDate(dates.get(1));
        dangerInspectPublic.setCreateUserId(MyShiroUtils.getSessionUserId());
        dangerInspectPublic.setType(Constant.DangerInspectType.SELF.getValue());
        List<DangerInspectPublic> dangerInspectPublics = dangerInspectPublicMapper.search(dangerInspectPublic
                , new ArrayList<>());
        int count = 0, yearCount = 12;
        if (CollectionUtil.isNotEmpty(dangerInspectPublics)) {
            count = dangerInspectPublics.size();
        }
        if (CollectionUtil.isNotEmpty(sysUserPosts)) {
            SysPost sysPost=sysPostMapper.selectByPrimaryKey(sysUserPosts.get(0).getPostId());
            if(ObjectUtil.isNotNull(sysPost)){
                if(sysPost.getSelfInspectTimeLimit().equals(Constant.SysPostTimeLimitType.WEEK.getValue())){
                    yearCount=48*sysPost.getSelfInspectCount();
                }
                if(sysPost.getSelfInspectTimeLimit().equals(Constant.SysPostTimeLimitType.MONTH.getValue())){
                    yearCount=12*sysPost.getSelfInspectCount();
                }
                if(sysPost.getSelfInspectTimeLimit().equals(Constant.SysPostTimeLimitType.QUARTER.getValue())){
                    yearCount=4*sysPost.getSelfInspectCount();
                }
                if(sysPost.getSelfInspectTimeLimit().equals(Constant.SysPostTimeLimitType.HALFYEAR.getValue())){
                    yearCount=2*sysPost.getSelfInspectCount();
                }
                if(sysPost.getSelfInspectTimeLimit().equals(Constant.SysPostTimeLimitType.YEAR.getValue())){
                    yearCount=sysPost.getSelfInspectCount();
                }
            }
        }
        Map<String,Integer> data=new HashMap<>();
        data.put("finishCount",count);
        data.put("yearCount",yearCount);
        result.setData(data);
        return result;
    }
}
